package com.hexus.eve.actions;

import java.util.Collections;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;

import com.hexus.eve.comparators.MarketGroupsComparator;
import com.hexus.eve.entities.MarketGroup;
import com.hexus.eve.utils.MarketGroupUtils;
import com.hexus.eve.utils.TranslationUtils;
import com.opensymphony.xwork2.ActionSupport;

public class SearchMarketGroups extends ActionSupport {
	private static final long serialVersionUID = 1L;
	private Integer id;
	private MarketGroup marketGroup;
	private List<MarketGroup> marketGroups;
	private List<MarketGroup> marketGroupParents;
	
	@Autowired
	private TranslationUtils translationUtils;
	
	@Autowired
	private MarketGroupUtils marketGroupUtils;
	
	@SuppressWarnings("unchecked")
	public String execute() {
		if(id != null) {
			marketGroup = marketGroupUtils.getMarketGroup(id);
			translationUtils.translate(marketGroup, getLocale());
			
			marketGroupParents = marketGroup.getParentsAsList();			
			translationUtils.translate(marketGroupParents, getLocale());
		}
			
		marketGroups = marketGroupUtils.getRelatedItems(id, MarketGroupUtils.CHILDREN);
		
		translationUtils.translate(marketGroups, getLocale());
		Collections.sort(marketGroups, new MarketGroupsComparator());
		return SUCCESS;
	}

	public Integer getId() {
		return id;
	}

	public void setId(Integer id) {
		this.id = id;
	}
	
	public List<MarketGroup> getMarketGroupParents(){		
		return marketGroupParents;
	}
	
	public List<MarketGroup> getMarketGroups(){
		return marketGroups;
	} 
	
	public MarketGroup getMarketGroup(){
		return marketGroup;
	}
}
